package com.zhazha.earlybird.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhazha.earlybird.beans.ProvinceInfo;

public class ProvinceDAO {

	private SQLiteDatabase mDatabase;
	private static final String PROVINCE_TABLE = "provinces";
	
	private static final String PROVINCE_ID 				= "id";
	private static final String PROVINCE_NAME				= "name";
	private static final String PROVINCE_SORT				= "sort";
	private static final String PROVINCE_REMARK				= "remark";
	
	private static final int PROVINCE_ID_INDEX 				= 0;
	private static final int PROVINCE_NAME_INDEX			= 1;
	private static final int PROVINCE_SORT_INDEX			= 2;
	private static final int PROVINCE_REMARK_INDEX			= 3;
	
	public ProvinceDAO(SQLiteDatabase db) {
		mDatabase = db;
	}
	
	
	public boolean insert(ProvinceInfo province) {
		
		try {
			ContentValues cv = new ContentValues();
			cv.put(PROVINCE_ID, 	province.getId());
			cv.put(PROVINCE_NAME,   province.getName());
			cv.put(PROVINCE_SORT,   province.getSort());
			cv.put(PROVINCE_REMARK, province.getRemark());
		
			this.mDatabase.insert(PROVINCE_TABLE, null, cv);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean delete(int id) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(PROVINCE_TABLE, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(int id, ContentValues cv) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.update(PROVINCE_TABLE, cv, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public ProvinceInfo queryById(int id) {
		ProvinceInfo province = null;
		
		try {
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(PROVINCE_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
				province = new ProvinceInfo();
				province.setId(c.getInt(PROVINCE_ID_INDEX));
				province.setName(c.getString(PROVINCE_NAME_INDEX));
				province.setSort(c.getInt(PROVINCE_SORT_INDEX));
				province.setRemark(c.getString(PROVINCE_REMARK_INDEX));
			}
			
			return province;
			
		}catch (Exception e) {
			e.printStackTrace();
			return province;
		}
	}
	
	public ProvinceInfo[] queryAll() {
		ProvinceInfo[] province = null;
		
		try {
			
			 Cursor c = this.mDatabase.query(PROVINCE_TABLE, null, null, null, null, null, null);
			
			if(c.moveToFirst()) {
				province = new ProvinceInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					province[i] = new ProvinceInfo();
					province[i].setId(c.getInt(c.getColumnIndex(PROVINCE_ID)));
					province[i].setName(c.getString(c.getColumnIndex(PROVINCE_NAME)));
					province[i].setSort(c.getInt(c.getColumnIndex(PROVINCE_SORT)));
					province[i].setRemark(c.getString(c.getColumnIndex(PROVINCE_REMARK)));
					c.moveToNext();
				}
				
			}
			
			return province;
			
		}catch (Exception e) {
			e.printStackTrace();
			return province;
		}
	}

}
